import { Component, OnInit } from '@angular/core';
import { RepairService } from "../../repair.service";
import { ActivatedRoute,Router } from '@angular/router';
@Component({
  selector: 'app-order-recive',
  templateUrl: './order-recive.component.html',
  styleUrls: ['./order-recive.component.css',
    '../../../../../assets/css/timeline.css']
})
export class OrderReciveComponent implements OnInit {

  username:string = '';
  orderId:string = '';
  data:any;
  mData = [];
  time:string = '';

  des:string = '';
  daochang:string = '';
  deid = {};
  constructor(private _router: Router,private activeRoute:ActivatedRoute,private repairService:RepairService) { }

  ngOnInit() {
    this.activeRoute.params.subscribe(
      params=> {
        this.orderId = this.repairService.orderId = params['orderId'];
        this.username = this.repairService.username;
        this.repairService.getData().subscribe(res => {
          this.data = res.result;
        }, err => console.log(err));

        this.repairService.getServiceMan().subscribe(res => {
          this.mData = res.result;
        });
        this.time = this.repairService.getTime();
      }
    );
  }

  fn($event,id){
    let checkbox = $event.target;
    if (checkbox.checked) {
      this.deid[id] = id;
    }else{
      delete this.deid[id];
    }
  }

  save(){
    let didstr="";
    let dids = Object.keys(this.deid);
    for(var i=0;i<dids.length;i++){
      didstr += dids[i];
      if(dids.length!=(i+1)){
        didstr +=",";
      }
    }

    if(this.daochang==""){
      alert("请选择是否到场！");
      return;
    }

    this.repairService.save('receive',{
      guzhang_id:this.orderId,
      daochang:this.daochang,
      miaoshu:this.des,
      wxrys:didstr
    }).subscribe(res => {
      alert("接警成功！");
      this._router.navigateByUrl('page/repair/order');
    },err => console.log(err));
  }

  cancel(){
    this.repairService.cancel();
  }

}
